package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

    ArrayList<Data> list = new ArrayList<>();
    ListView listView;
    Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = findViewById(R.id.lv);
        handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if((int) msg.obj== 100){
                    listView.setAdapter(new MyAdper());
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                            intent.putExtra("link", list.get(position).getLink());
                            startActivity(intent);
                        }
                    });
                }
            }
        };
        MyAdper myAdper = new MyAdper();
        listView.setAdapter(myAdper);
        new Thread(){
            @Override
            public void run() {
                try {
                    URL url = new URL("https://www.wanandroid.com/article/list/0/json");
                    HttpURLConnection coon = (HttpURLConnection) url.openConnection();
                    coon.setRequestMethod("GET");
                    coon.setConnectTimeout(5000);
                    if (coon.getResponseCode() == 200){
                        InputStream inputStream = coon.getInputStream();
                        InputStreamReader input = new InputStreamReader(inputStream);
                        BufferedReader bf = new BufferedReader(input);
                        StringBuffer sbf = new StringBuffer();
                        String n;
                        while ((n = bf.readLine())!=null){
                            sbf.append(n);
                        }
                        String str = sbf.toString();
                        JSONObject jsonObject = new JSONObject(str);
                        if((jsonObject.optInt("errorCode")==0)){
                            JSONObject data = jsonObject.getJSONObject("data");
                            JSONArray array = data.getJSONArray("datas");
                            for (int i = 0; i < array.length(); i++) {
                                JSONObject object = array.getJSONObject(i);
                                String title = object.optString("title");
                                String link = object.optString("link");
                                list.add(new Data(title, link));
                            }
                            Message message = new Message();
                            message.obj = 100;
                            handler.sendMessage(message);
                        }
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }.start();
    }
    class MyAdper extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(MainActivity.this,R.layout.item_one, null);
            TextView textView = view.findViewById(R.id.tv_item);
            textView.setText(list.get(position).getTitle());
            return view;
        }
    }
}